/*
 * 描述 资金接口
 * Create by cy on 2018/10/25
 * */

const express = require("express");
const router = express.Router();
const db = require("../../db");

router.get("/t", (req, res) => {
  const { pageName } = req.query;
  console.log(pageName);
  let s = `SELECT * FROM articles`;

  db.query(s, function (error, result) {
    if (error) throw error;
    res.json({
      data: result,
    });
  });
});
/*
 * $param
 * @return
 * @desc
 * */

router.get("/", (req, res) => {
  const { pageName } = req.query;
  console.log(pageName);
  let s = `SELECT * from page_szzj WHERE pageName ="${pageName}"`;

  db.query(s, function (error, result) {
    if (error) throw error;
    res.json({
      data: result?.map((item) => {
        return {
          ...item,
          json: JSON.parse(item?.json),
        };
      }),
    });
  });
});

router.get("/szzj/workTile/queryTaskGroupByBoard", (req, res) => {
  const { boardId } = req.query;
  let s = `select * from board_group WHERE boardId=${
    boardId || 1
  } ORDER BY groupOrder;`;
  let s_task = `select * from board_task ORDER BY taskOrder;`;
  console.log(12, s, boardId);

  db.query(s, function (error, result) {
    if (error) throw error;
    db.query(s_task, (err, res1) => {
      if (err) throw err;
      let data = result?.map((item) => {
        return {
          ...item,
          taskList: res1?.filter((e) => e?.groupId == item?.id),
        };
      });
      console.log(data, 266);

      res.json({
        data,
      });
    });
  });
});
router.post("/szzj/workTile/moveGroup", (req, res) => {
  const { taskGroupList } = req.body;
  taskGroupList?.map((item, index) => {
    let i = `UPDATE board_group SET groupOrder=${item?.groupOrder} WHERE id=${item?.id};`;
    db.query(i, function (error, result) {
      if (error) throw error;
      if (index == taskGroupList?.length - 1) {
        res.json(result);
      }
    });
  });

  //   let i = `UPDATE board_group su1
  // JOIN board_group su2 ON ( su1.order_id = ${source} AND su2.order_id = ${destination} )
  // OR ( su1.order_id = ${destination} AND su2.order_id = ${source} )
  // SET su1.order_id = su2.order_id,
  // su2.order_id = su1.order_id;`;
  //
  //   db.query(i, function (error, result) {
  //     if (error) throw error;
  //     res.json(result);
  //   });
});
router.post("/szzj/workTile/moveTask", (req, res) => {
  const { taskSimpleDTOList } = req.body;
  taskSimpleDTOList?.map((item, index) => {
    let i = `UPDATE board_task SET groupId=${item?.groupId},taskOrder=${item?.taskOrder} WHERE id=${item?.id};`;
    db.query(i, function (error, result) {
      if (error) throw error;
      if (index == taskSimpleDTOList?.length - 1) {
        res.json(result);
      }
    });
  });
});
router.post("/szzj/workTile/addOrUpdateBoardTaskGroup", (req, res) => {
  const { taskGroupTitle, progress, id, groupOrder, boardId } = req.body;
  let i = `insert into board_group (taskGroupTitle,progress,groupOrder,boardId) value ("${taskGroupTitle}",${progress},${groupOrder},${boardId});`;
  if (id) {
    i = `UPDATE board_group SET 
        taskGroupTitle="${taskGroupTitle}"
      WHERE id=${id}`;
  }
  db.query(i, function (error, result) {
    if (error) throw error;
    res.json(result);
  });
});
router.post("/szzj/workTile/delTaskGroup", (req, res) => {
  const { id } = req.body;
  let d = `DELETE from board_group WHERE id=${id}`;
  console.log(d);
  db.query(d, function (error, result) {
    if (error) throw error;
    res.json({
      data: result || [],
    });
  });
});
router.post("/szzj/workTile/addOrUpdateBoardTask", (req, res) => {
  const {
    taskTitle,
    id,
    taskStatus,
    taskBeginDate,
    taskEndDate,
    tag,
    taskPriority,
    taskDesc,
    author,
    taskOrder,
    groupId,
  } = req.body;
  // let i=` insert into board_task (taskTitle,groupId,taskOrder) value ("${taskTitle}",${groupId},${taskOrder});`
  let key = ``;
  let value = ``;
  let update = ``;
  Object.entries(req.body)?.map((item) => {
    if (!["id", "timeStamp", "randomNum"]?.includes(item?.[0])) {
      key = item?.[0] + "," + key;
      value = `"${item?.[1]}"` + "," + value;
      update = `${item[0]}="${item[1]}"` + "," + update;
    }
  });
  let i = `insert into board_task (${key.substring(
    0,
    key.lastIndexOf(",")
  )}) value (${value.substring(0, value.lastIndexOf(","))});`;
  let u = `UPDATE board_task SET ${update.substring(
    0,
    update.lastIndexOf(",")
  )} WHERE id=${id}`;
  console.log(update.substring(0, update.lastIndexOf(",")), 103);
  if (id) {
    i = `UPDATE board_task SET
        taskTitle="${taskTitle}",
        taskStatus="${taskStatus}"
      WHERE id=${id}`;
    i = u;
  }
  db.query(i, function (error, result) {
    if (error) throw error;
    res.json(result);
  });
});
router.post("/szzj/workTile/delTask", (req, res) => {
  const { id } = req.body;
  let d = `DELETE from board_task WHERE id=${id}`;
  console.log(d);
  db.query(d, function (error, result) {
    if (error) throw error;
    res.json({
      data: result || [],
    });
  });
});

router.get("/szzj/page", (req, res) => {
  const { boardId } = req.query;
  let s = `select * from page_szzj `;

  db.query(s, function (error, result) {
    if (error) throw error;
    res.json({
      data: result,
    });
  });
});
router.post("/szzj/addOrUpdatePage", (req, res) => {
  const { json, id, pageName } = req.body;

  let i = `INSERT into  page_szzj (pageName,json) value ("${json}","${pageName}")`;
  if (id) {
    i = `UPDATE page_szzj SET 
        json='${json}'
        WHERE id=${id}`;
    console.log(i);
  }
  db.query(i, function (error, result) {
    if (error) throw error;
    res.json(result);
  });
});

module.exports = router;
